/*
    Dominate 3D - A cube-based game of life

    Copyright (C) 2007, Craig H. Miller (kidmosey@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CLIENT_H__
#define __CLIENT_H__

#include "net.h"

#include <map>
#include <string>
#include <vector>

class Player;

class Client
{
public:
    typedef void (*MessageFunc)(Client &, const std::vector<SocketParam> &);

    // connect to a server (for the client interface)
    //Client(const char addr[], int port, void (*ProgressFunc)(const std::string &) = 0);
    Client();

    // bind to an existing socket (for the server interface)
    Client(SOCKET sock): m_sock(sock), m_id((int )sock)
    {
        // send the client our id
        u_long id = htonl(m_id);
        send(m_sock, (char *)&id, sizeof(u_long), 0);
    }

    ~Client()
    {
        //assert(m_player);
        closesocket(m_sock);
    }

    void SendMessage(const TMessage type, const std::vector<SocketParam> &params);
    void ReadMessage(std::map<TMessage, MessageFunc> &messageMap);
    void RegisterPlayer();
    void RegisterBot(const std::string &name);

    int Connect(const char addr[], int port);

    int GetID() const { return m_id; }

    SOCKET GetSocket() const { return m_sock; }

private:
    struct
    {
        TMessage type;
        std::vector<SocketParam> params;
    } TServerMessage;

    SOCKET m_sock;
    int    m_id;
};

#endif // __CLIENT_H__
